package com.cms.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cms.common.MD5Util;
import com.cms.common.SiteUtil;
import com.cms.common.StringUtils;
import com.cms.constant.CmsConstant;
import com.cms.constant.CmsSeqConstant;
import com.cms.model.ActivityCommission;
import com.cms.model.TrAdminUser;
import com.cms.model.TrUser;
import com.cms.model.UserCoinLog;
import com.cms.service.MemberUserService;
import com.cms.service.ReportService;
import com.google.gson.Gson;

@Controller
public class UserController extends BaseController {

	@Autowired
	private MemberUserService memberUserService;

	@Autowired
	private ReportService reportService;

	@RequestMapping("/userMain.do")
	public String enter(HttpServletRequest req, HttpServletResponse resp,
			ModelMap map) throws Exception {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put(CmsConstant.CATALOG_NAME, CmsConstant.USER_STATUS);
		map.put("USER_STATUS", this.reportService.getReportResult( CmsConstant.CATALOG_CODE, param));
		param.put(CmsConstant.CATALOG_NAME, CmsConstant.USER_TYPE);
		map.put("USER_TYPES", this.reportService.getReportResult( CmsConstant.CATALOG_CODE, param));
		return "user/userMain.html";
	}

	@RequestMapping("/userMain/toRegUser.do")
	public String toRegUser(HttpServletRequest req, HttpServletResponse resp,
			ModelMap map) throws Exception { 
		return "user/userInfo.html";
	}
	
	@RequestMapping("/userMain/toEditUser.do")
	public String toEditUser(HttpServletRequest req, HttpServletResponse resp,
			ModelMap map) throws Exception { 
		Map<String,Object>param = this.buildQueryMap(req);
		map.put("user", this.reportService.querySingleResult("getUserInfo", param)); 
		return "user/editUserInfo.html";
	}
	
	
	@RequestMapping("/userMain/updateUserInfo.do")
	public void updateUserInfo(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception {
	    TrAdminUser tu = this.getLoginUser(req);
	    TrUser user = new TrUser();
	    user.setModifyUser(tu.getId());
	    Map<String,Object>result = new HashMap<String, Object>();
	    try
	    {
	    	 this.export(req, "user", user);
	    	 if(!StringUtils.isBlank(user.getUserPassword()))
	    	 {
	    		 user.setUserPassword(MD5Util.MD5(user.getUserPassword()));
	    	 }
	    	 if(!StringUtils.isBlank(user.getCoinPassword()))
	    	 {
	    		 user.setCoinPassword(MD5Util.MD5(user.getCoinPassword()));
	    	 }
	    	 user.setFanDian(user.getFanDian()/100);
	    	 this.memberUserService.updateUserInfo(user);
	    	 result.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
	    }
	    catch (Exception e) {
			 result.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			 result.put(CmsConstant.MSG, e.getMessage());
		}
	    this.renderText(resp, new Gson().toJson(result));
	}
	
	@RequestMapping("/userMain/registerUser.do")
	public void registerUser(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception {
	    TrAdminUser tu = this.getLoginUser(req);
	    TrUser user = new TrUser();
	    user.setCreateUser(tu.getId());
	    user.setModifyUser(tu.getId());
	    Map<String,Object>result = new HashMap<String, Object>();
	    try
	    {
	    	 this.export(req, "user", user);
	    	 TrUser t = this.memberUserService.getUserInfoByLoginName(user.getLoginName());
	         if(t==null)
	         {
	        	 user.setUserPassword(MD5Util.MD5(user.getUserPassword()));
		         user.setRegisterAddr(SiteUtil.getIpAddr(req));
		         user.setFanDian(user.getFanDian()/100);
		         user.setCoin(0d);
		         user.setFcoin(0d);
		         user.setIsOnline(1);
		    	 this.memberUserService.registerUser(user);
		    	 result.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
	         }
	         else
	         {
	        	 result.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
				 result.put(CmsConstant.MSG, t.getLoginName()+"已在平台注册!");
	         }
	    }
	    catch (Exception e) {
			 result.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			 result.put(CmsConstant.MSG, e.getMessage());
		}
	    this.renderText(resp, new Gson().toJson(result));
	}
	
	@RequestMapping("/userMain/userList.do")
	public String userList(HttpServletRequest req, HttpServletResponse resp,
			ModelMap map) throws Exception {
		Map<String, Object> param = buildQueryMap(req);
		map.put("userList", this.reportService.pagingReportResult("pagingUserList", param));
		return "user/userList.html";
	}

	@RequestMapping("/userMain/changeUserStatus.do")
	public void changeUserStatus(HttpServletRequest req,
			HttpServletResponse resp, ModelMap map) throws Exception {
		TrUser user = new TrUser();
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			this.memberUserService.changeUserStatus(user);
			result.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
		} catch (Exception e) {
			result.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
			result.put(CmsConstant.MSG, e.getMessage());
		}
		this.renderText(resp, new Gson().toJson(result));
	}
	
	@RequestMapping("/userMain/doRecharge.do")
	public String doRecharge(HttpServletRequest req, HttpServletResponse resp,
			ModelMap map) throws Exception {
	    Map<String,Object>param =new HashMap<String, Object>();
		param.put(CmsConstant.CATALOG_NAME, "RUN_ACCOUNT");
		map.put("runningAccount", this.reportService.getReportResult(CmsConstant.CATALOG_CODE, param));
		param.putAll(buildQueryMap(req));
		List list =this.reportService.getReportResult("getAccountInfo", param);
		if(list!=null&&!list.isEmpty())
		{
			map.put("account",list.get(0));
		}
		else
		{
			map.put("account",param);
		} 
		return "user/userRechange.html";
	}

	
	@RequestMapping("/userMain/recharge.do")
	public void recharge(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception 
	{ 
		UserCoinLog coinLog = new UserCoinLog();
		this.export(req, "account", coinLog); 
		TrUser user = new TrUser();
		user.setId(coinLog.getUid());
		user.setCoin(coinLog.getCoin()); 
		user.setLoginName(coinLog.getUname());
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			TrAdminUser admin = this.getLoginUser(req);
			coinLog.setUserCoin(coinLog.getUserCoin()+user.getCoin());
			coinLog.setActionAddr(SiteUtil.getIpAddr(req));
			coinLog.setActionUid(admin.getId());
			coinLog.setActionDate(new Date());
			String chargeOrder= this.reportService.queryCmsSeq(CmsSeqConstant.RECHARGE);
			coinLog.setCoinOrder(chargeOrder);
			coinLog.setRemark("管理员充值");
			this.memberUserService.reCharge(user, coinLog);
			result.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
		} catch (Exception e) {
			result.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			result.put(CmsConstant.MSG, e.getMessage());
		}
		this.renderText(resp, new Gson().toJson(result));
	}
	
	
	@RequestMapping("/userMain/statisticsUser.do")//会员统计信息
	public String statisticsUser(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception 
	{
		map.put("user",this.reportService.querySingleResult("statisticsUser", this.buildQueryMap(req)));
	    return "user/userStatistics.html";
	}
	
	
	@RequestMapping("/userMain/toSetAcomm.do")//活动佣金设置
	public String toSetAcomm(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception 
	{
		int userId =StringUtils.str2Int(req.getParameter("id"));
		TrUser user = memberUserService.getUserInfoById(userId);
	    map.put("user", user);
	    Map<String,Object>param = new HashMap<String, Object>();
	    param.put("uid", user.getUid());
	    List<Map>result=  this.reportService.getReportResult("queryAcommCond", param);
	    if(result!=null&&!result.isEmpty())
	    {
	    	map.put("chargeOrderDetail", result.get(0));
	    }
	    else
	    {
	    	map.put("chargeOrderDetail", new HashMap<String,Object>());
	    }
		return "user/userSetAcomm.html";
	}
	
	
	@RequestMapping("/userMain/setAcomm.do")//活动佣金设置
	public void setAcomm(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception 
	{
		   ActivityCommission commission = new ActivityCommission();
		   this.export(req, "comm", commission);
		   Map<String, Object> result = new HashMap<String, Object>();
		   try {
				TrAdminUser admin = this.getLoginUser(req); 
				commission.setCreateUser(admin.getId());
				commission.setModifyUser(admin.getId());
				commission.setStatus(0);
				commission.setCommissionRate(commission.getCommissionRate()/100);
				commission.setCoinAmount(commission.getChargeAmount()*(1+commission.getCommissionRate())*commission.getCommissionBeishu());
				this.memberUserService.saveActivityCommission(commission);
				result.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
			} catch (Exception e) {
				result.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
				result.put(CmsConstant.MSG, e.getMessage());
			}
			this.renderText(resp, new Gson().toJson(result));
	}
	
	
	@RequestMapping("/userMain/clearAcomm.do")//清除活动佣金设置
	public void clearAcomm(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception 
	{ 
		 TrAdminUser user = SiteUtil.getCurrentUser();
		 ActivityCommission comm =new ActivityCommission();
		 comm.setUid(req.getParameter("uid"));
		 comm.setModifyUser(user.getId());
		 Map<String, Object> result = new HashMap<String, Object>();
		 try
		 {
			 this.memberUserService.deleteActivityCommission(comm);
			 result.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
		 } catch (Exception e) {
				result.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
				result.put(CmsConstant.MSG, e.getMessage());
		 }
		 this.renderText(resp, new Gson().toJson(result));
	}
	
	@RequestMapping("/userMain/pagingUserSubProxy.do") // 查询下级信息
	public String pagingSubProxy(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception 
	{ 
		Map<String, Object> param = buildQueryMap(req);
		map.put("userList", this.reportService.pagingReportResult("pagingUserSubProxy", param));
		return "user/userList.html";
	}
	
	
	@RequestMapping("/userMain/pagingUserSuperProxy.do")//查询上级信息
	public String pagingSuperProxy(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception 
	{
		Map<String, Object> param = buildQueryMap(req);
		map.put("userList", this.reportService.pagingReportResult("pagingUserSuperProxy", param));
		return "user/userList.html";
	}
	
	
	
}
